/**
 * Licensed to JumpMind Inc under one or more contributor
 * license agreements.  See the NOTICE file distributed
 * with this work for additional information regarding
 * copyright ownership.  JumpMind Inc licenses this file
 * to you under the GNU General Public License, version 3.0 (GPLv3)
 * (the "License"); you may not use this file except in compliance
 * with the License.
 *
 * You should have received a copy of the GNU General Public License,
 * version 3.0 (GPLv3) along with this library; if not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jumpmind.symmetric.notification;

import java.util.List;
import java.util.Map;

import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.ext.ISymmetricEngineAware;
import org.jumpmind.symmetric.model.Monitor;
import org.jumpmind.symmetric.model.MonitorEvent;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NotificationTypeLog implements INotificationType, ISymmetricEngineAware, IBuiltInExtensionPoint {

    private final Logger log = LoggerFactory.getLogger(getClass());
    
    protected ISymmetricEngine engine;
    
    public void notify(Notification notification, List<MonitorEvent> monitorEvents) {
        Map<String, Node> nodes = engine.getNodeService().findAllNodesAsMap();

        for (MonitorEvent monitorEvent : monitorEvents) {
            Node node = nodes.get(monitorEvent.getNodeId());
            String nodeString = node != null ? node.toString() : monitorEvent.getNodeId();
            String message = "Monitor " + monitorEvent.getType() + " on " + nodeString + " reached threshold of "
                    + monitorEvent.getThreshold() + " with a value of "+ monitorEvent.getValue();
            if (monitorEvent.getSeverityLevel() >= Monitor.SEVERE) {
                log.error(message);
            } else if (monitorEvent.getSeverityLevel() >= Monitor.WARNING) {
                log.warn(message);
            } else {
                log.info(message);
            }
        }
    }

    @Override
    public String getName() {
        return "log";
    }

    @Override
    public void setSymmetricEngine(ISymmetricEngine engine) {
        this.engine = engine;
    }

}
